import addressApi from "../../api/avatar/avatar.js"
const address = {
	namespaced: true,
	state: {
		addressList: [],
		province: [],
		city: [],
		county: []
	},
	getters: {
		defaultAddressList(state) {
			return state.addressList.sort((obj1, obj2) =>
				obj2.isDefault - obj1.isDefault
			)
		}
	},
	mutations: {
		SET_ADDRESSLIST(state, pyload) {
			state.addressList = pyload
		},
		SET_PROVINCE(state, pyload) {
			state.province = pyload
		},
		SET_CITY(state, pyload) {
			state.city = pyload
		},
		SET_COUNTY(state, pyload) {
			state.county = pyload
		},
	},
	actions: {
		setAddressList({
			commit
		}, pyload) {
			return new Promise((resolve, reject) => {
				addressApi.getAddressList(pyload).then(res => {
						if (res.code == 200) {
							commit('SET_ADDRESSLIST', res.list)
							resolve(res.list)
						} else {
							reject()
						}
					})
					.catch(err => {
						// 暂时未找到mock返回promise.reject()的方法
						return reject(err)
					})
			})
		},
		setProvince({
			commit
		}, pyload) {
			return new Promise((resolve, reject) => {
				addressApi.findProvince(pyload).then(res => {
						if (res.code == 200) {
							commit('SET_PROVINCE', res.list)
							resolve(res.list)
						} else {
							reject()
						}
					})
					.catch(err => {
						// 暂时未找到mock返回promise.reject()的方法
						return reject(err)
					})
			})
		},
		setCity({
			commit
		}, pyload) {
			return new Promise((resolve, reject) => {
				addressApi.findCity(pyload).then(res => {
						if (res.code == 200) {
							commit('SET_CITY', res.list)
							resolve(res.list)
						} else {
							reject()
						}
					})
					.catch(err => {
						// 暂时未找到mock返回promise.reject()的方法
						return reject(err)
					})
			})
		},
		setCounty({
			commit
		}, pyload) {
			return new Promise((resolve, reject) => {
				addressApi.findCounty(pyload).then(res => {
						if (res.code == 200) {
							commit('SET_COUNTY', res.list)
							resolve(res.list)
						} else {
							reject()
						}
					})
					.catch(err => {
						// 暂时未找到mock返回promise.reject()的方法
						return reject(err)
					})
			})
		},
	}
}

export default address